﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Web.Framework;
using System.Web.UI.WebControls;

namespace WebAppPlanProduccion.Code
{
    public abstract partial class BaseMasterPage : System.Web.UI.MasterPage, IWebMessage
    {
        public static int ApplicationId
        {
            get
            {
                return Convert.ToInt32(HttpContext.Current.Session["appId"]);
            }
            set
            {
                HttpContext.Current.Session["appId"] = value;
            }
        }

        public static int CompanyId
        {
            get
            {
                return Convert.ToInt32(HttpContext.Current.Session["companyId"]);
            }
            set
            {
                HttpContext.Current.Session["companyId"] = value;
            }
        }

        public static string UserName
        {
            get
            {
                return Convert.ToString(HttpContext.Current.Session["UserName"]);
            }
            set
            {
                HttpContext.Current.Session["UserName"] = value;
            }
        }

        public static bool IsAdmin
        {
            get
            {
                return Convert.ToBoolean(HttpContext.Current.Session["IsAdmin"]);
            }
            set
            {
                HttpContext.Current.Session["IsAdmin"] = value;
            }
        }

        private List<string> innerOperationsForUser = new List<string>();
        private List<string> OperationsForUser
        {
            get
            {                
                return innerOperationsForUser;
            }
        }

        /// <summary>
        /// Testea si el usuario tiene permisos para la operación
        /// </summary>
        /// <param name="operation"></param>
        /// <returns></returns>
        public bool HasPermissionFor(string operation)
        {
            return BaseMasterPage.IsAdmin || OperationsForUser.Where(o => o == operation).Count() > 0;
        }
       
        public abstract void ShowMessage(string message, WebMessageType type);

        public abstract void ShowMessage(string title, string message, WebMessageType type);

        public abstract void MakeMenuVisible(bool visible);

        public abstract void BuildMenu();

        public virtual void PreLoad(List<string> operations)
        {
            innerOperationsForUser = operations;
        }
    }
}